package com.cyan.app.workflow.activity.TQM;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by owen on 2019-11-27.
 */

public class TQMBadReasonActivity extends BaseActivity {
    public static final String TAG = "TQMBadReasonActivity";
    private ListView miscode_lv;
    private PullDownView mPullDownView;
    private List<TQMBadReasonActivity.BadReason> mMiscodeBeanList;
    private TQMBadReasonActivity.BadReasonAdapter adapter;
    private String tqmKind;
    private  String procedureNo;
    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_list3_head);
        tqmKind= getIntent().getStringExtra("TQMKind");
        procedureNo = getIntent().getStringExtra("ProcedureNo");
        titleBar();
        /*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
        mPullDownView = (PullDownView) findViewById(R.id.layout_list3_head_list);
        miscode_lv = mPullDownView.getListView();
        //prossinfo_lv.setOnItemClickListener(this);
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        mPullDownView.setHideFooter();
        TQMBadReasonActivity.AsyncLoader task = new TQMBadReasonActivity.AsyncLoader();
        task.execute(tqmKind,procedureNo);
    }

    public class AsyncLoader extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            showdialog.showDialog(TQMBadReasonActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String action="GetTQMBadReason";
            try{
                JSONObject param = new JSONObject();
                param.put("TQMKind", params[0]);
                param.put("ProcedureNo", params[1]);
                param.put("FactoryID", HttpConfig.Main_FactoryID);
                param.put("UserNo", AppData.getInstance().getUserId());
                param.put("Connectstring", AppData.getInstance().getConnectstring());
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, action, param.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            if (s == null) {
                return;
            }
            BadReasonBean commonBean = new Gson().fromJson(s, BadReasonBean.class);
            if (commonBean.getCode() < 0) {
                AppUtils.showTip(TQMBadReasonActivity.this, commonBean.getMsg());
            } else {
                mMiscodeBeanList = commonBean.getResult();
                startProssThread();
            }
            showdialog.closeDialog();
        }
    }

    private void startProssThread() {
        adapter = new TQMBadReasonActivity.BadReasonAdapter(this);
        miscode_lv.setAdapter(adapter);
    }
    //头部信息
    private void titleBar() {
        ((TextView) findViewById(R.id.top_title_info)).setText("不良原因类型");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    public class BadReasonAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;

        public BadReasonAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mMiscodeBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
           // if(convertView==null) {
                holder = new ViewHolder();
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.CENTER_VERTICAL;
                convertView = inflater.inflate(R.layout.layout_list3_list, null);
                holder.BadReason_code = (TextView) convertView.findViewById(R.id.layout_list3_list_code);
                holder.BadReason_cdesc = (TextView) convertView.findViewById(R.id.layout_list3_list_value);
                holder.BadReason_selected =(CheckBox) convertView.findViewById(R.id.layout_list3_list_selected);

                holder.BadReason_code.setText(mMiscodeBeanList.get(position).getBadCode());
                holder.BadReason_cdesc.setText(mMiscodeBeanList.get(position).getBadDesc());
                convertView.setTag(holder);
           // }else {
            //    holder = (ViewHolder) convertView.getTag();
            //}
            holder.BadReason_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = TQMBadReasonActivity.this.getIntent();
                    intent.putExtra("result",mMiscodeBeanList.get(position).getBadCode());
                    TQMBadReasonActivity.this.setResult(RESULT_OK, intent);
                    finish();
                }
            });
            return convertView;
        }

        class ViewHolder {
            TextView BadReason_code;
            TextView BadReason_cdesc;
            CheckBox BadReason_selected;
        }
    }

    class BadReasonBean extends BaseBean {
        private List<TQMBadReasonActivity.BadReason> Result;

        public List<TQMBadReasonActivity.BadReason> getResult() {
            return Result;
        }

        public void setResult(List<TQMBadReasonActivity.BadReason> data) {
            Result = data;
        }
    }
    class BadReason {
        private String BadCode;
        private String BadDesc;
        private int TQMKind;
        public String getBadCode() {
            return BadCode;
        }
        public void setBadCode(String _code) {
            BadCode = _code;
        }
        public String getBadDesc() {
            return BadDesc;
        }
        public void setBadDesc(String _cdesc) {
            BadDesc = _cdesc;
        }
        public int getTQMKind() {
            return TQMKind;
        }
        public void setTQMKind(int kind) {
            TQMKind = kind;
        }
    }
}
